import React from 'react';
import styles from './index.less';
import { App, ConfigProvider } from 'antd';
import { Outlet, useModel, history } from '@umijs/max';
import { flushSync } from 'react-dom';
import { getCurrentUser } from '@/requestErrorConfig';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
const User: React.FC = () => {
  const { initialState, setInitialState } = useModel('@@initialState');

  const fetchUserInfo = async () => {
    const userInfo = await initialState?.fetchUserInfo?.();
    if (userInfo) {
      flushSync(() => {
        setInitialState((s) => ({
          ...s,
          currentUser: userInfo,
        }));
      });
    }
  };
  const authDirect = async () => {
    await fetchUserInfo();
    history.replace('/');
  };

  const token = getCurrentUser();

  if (token) {
    authDirect();
    return null;
  }
  return (
    <ConfigProvider
      theme={{
        token: {
          colorPrimary: '#0D70D8',
          colorPrimaryText: '#0D70D8',
        },
      }}
    >
      <App>
        <div className={styles.container}>
          <Outlet />
        </div>
        <ToastContainer />
      </App>
    </ConfigProvider>
  );
};

export default User;
